
#include "stdafx.h"
#include "image.h"

namespace_iRing_using


HRGN RGN::FromImage( CImage const& image, COLORREF const& KeyColor )
{
	const int iWidth = image.GetWidth();
	const int iHeight = image.GetHeight();

	HRGN hRgn = ::CreateRectRgn (0, 0, iWidth, iHeight);
	for (int y = 0; y < iHeight; ++y)
	{
		int iLastX = 0;
		BOOL bStart = FALSE;
		for (int x = 0 ; x < iWidth; ++x)
		{
			if ( KeyColor == image.GetPixel(x, y) )
			{
				if (bStart)
				{
					HRGN hLineRgn = ::CreateRectRgn( iLastX, y, x, y + 1 );
					::CombineRgn( hRgn, hRgn, hLineRgn, RGN_DIFF );
					::DeleteObject (hLineRgn);
					bStart = FALSE;
				}
			}
			else
			{
				if (!bStart)
				{
					bStart = TRUE;
					iLastX = x;
				}
			}
		}

		if (bStart)
		{
			HRGN hLineRgn = ::CreateRectRgn ( iLastX, y, iWidth, y + 1 );
			::CombineRgn( hRgn, hRgn, hLineRgn, RGN_DIFF );
			::DeleteObject(hLineRgn);
		}
	}
	return hRgn;
}

CImage RARGB::Pargb( CImage const& image )
{
	if ( image.IsTransparencySupported() )
		return image;

	CImage image2 = image;
	const int iWidth = image2.GetWidth();
	const int iHeight = image2.GetHeight();
	for(int i = 0; i < iWidth; i++)
	{
		for(int j = 0; j < iHeight; j++)
		{
			unsigned char* p = reinterpret_cast<unsigned char*>(image2.GetPixelAddress(i , j));   
			p[0] = p[0] * p[3] / 255;
			p[1] = p[1] * p[3] / 255;
			p[2] = p[2] * p[3] / 255;
		}
	}
	return image2;
}